// pages/customer_information/customer_information.js
var App = getApp();
function jsonSort(jsonObj) {
    let arr = [];
    for (var key in jsonObj) {
        arr.push(key) 
    }
    arr.sort();
    let str = '';
    for (var i in arr) {
        str += arr[i] + "=" + jsonObj[arr[i]] + "&"
    } 
    return str.substr(0, str.length - 1)
}
Page({
    // onReady: function (e) {
    //     // 使用 wx.createAudioContext 获取 audio 上下文 context
    //     this.audioCtx = wx.createAudioContext('myAudio')
    // },
    /**
     * 页面的初始数据
     */
    data: {
        many_in: true,//更多信息
        more: '更多信息',
        type:"",//判断潜客还是会员
        id: '',//
        caller:{},//潜客各种信息
        yc:'',
        level: '',//判断是否为经理，控制显示隐藏， 1普通会籍 2会籍主管 3会籍经理
        whereFrom: ['新展', '轮排', '转介绍', '大众点评', '今日头条', '公众号', '电话咨询', '广告', '微信', '公开课', '开放日', '体验卡', '美团'],//来源
        num: "",//维护记录数
        describe: [],//维护记录
        tail_metood: ['默认', '电话跟进', '微信跟进', '面对面跟进', '其他跟进'],//跟进方式
        arrive: [],//是否到店
        yundong: ['0', '腰腹', '减重', '心肺功能提升', '塑性', '体态调整', '疼痛康复', '增加肌肉', '增加力量', '提高柔韧度', '放松压力'],//运动需求与期望效果
        flag_screen: true,
        // 时间组件
        year: 0,
        month: 0,
        date: ['日', '一', '二', '三', '四', '五', '六'],
        dateArr: [],
        isToday: 0,
        isTodayWeek: false,
        todayIndex: 0,
        index1:'',
        member_record:[],
        path_auto:'',
        show_bus:false,

    },
   

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var This = this;
        This.setData({
            id: options.id,
            type:options.type,
        })
        var now = new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        this.dateInit();
        This.setData({
            id: options.id,
            year: year,
            month: month,
            isToday: '' + year + month + now.getDate()
        })
        if (This.data.type == 1) {
            wx.setNavigationBarTitle({
                title: '普通会员详情'
            })
        } else if (This.data.type == 2) {
            wx.setNavigationBarTitle({
                title: '私教会员详情'
            })
        }
        this.attendance_record();
        this.loade();
        console.log(This.data.type)
        console.log(This.data.dateArr)
    },
    // 点击shou卡
    click_imgs(){
        var This= this;
        This.setData({
            show_bus: !This.data.show_bus
        })
    },
    // 播放
    play(e){
        var This = this;
        var index2 = e.currentTarget.dataset.index;
        console.log(index2)
        for (var index in This.data.member_record) {
            if (This.data.member_record[index].index1 == true){
                // This.setData({
                //     action: {
                //         method: 'pause'
                //     },
                // });
                This.data.member_record[index].index1 = false;
            }else {
                This.data.member_record[index].index1 = false;
            }
            
        }
        This.data.member_record[index2].index1 = true;
        This.setData({
            path_auto: This.data.member_record[index2].path,
            // action: {
            //     method: 'play'
            // },
            member_record: This.data.member_record,
        });
        const back = wx.getBackgroundAudioManager();
        back.src = This.data.path_auto;
        back.title = "音频";
        back.play();
        back.onPlay(() => {
            console.log("音乐播放开始");
        })
        back.onEnded(() => {
            console.log("音乐播放结束");
        })
    },
    // 停止播放
    stop(e){
        var This = this;
        var index = e.currentTarget.dataset.index;
        for (var index in This.data.member_record){
            if (This.data.member_record[index].index1 == true) {
                // This.setData({
                //     action: {
                //         method: 'pause'
                //     },
                // });
                This.data.member_record[index].index1 = false;
            } else {
                This.data.member_record[index].index1 = false;
            }
        }
        This.data.member_record[index].index1 = false;
        console.log(e)
        This.setData({
            // action: {
            //     method: 'pause'
            // },
            member_record: This.data.member_record
        })
        wx.pauseBackgroundAudio();
    },
    //更多信息显示隐藏
    many_information: function () {
        var This = this;
        if (This.data.many_in) {
            This.setData({
                many_in: false,
                more: '收起信息'
            })
        } else {
            This.setData({
                many_in: true,
                more: '更多信息'
            })
        }
    },
    //会员卡显示隐藏
    vip_infor: function () {
        var This = this;
        if (This.data.vip) {
            This.setData({
                vip: false,
            })
        } else {
            This.setData({
                vip: true,
            })
        }
    },
    //私教显示隐藏
    sj_information: function () {
        var This = this;
        if (This.data.sj) {
            This.setData({
                sj: false,
            })
        } else {
            This.setData({
                sj: true,
            })
        }
    },
    loade: function () {
        var This = this;
        if(This.data.type == 1){
            App._get('coach/Maintain/get_member_detail', {
                store_id: wx.getStorageSync('store_id'),
                openid: wx.getStorageSync('openid'),
                phone: wx.getStorageSync('phone'),
                member_id: This.data.id,
                // member_id: 220424
            }, function (res) {
                This.setData({
                    member_record: []
                })
                for (var index in res.data.member_record){
                    res.data.member_record[index].index1 = false;
                }
                This.setData({
                    caller: res.data,
                    member_record: res.data.member_record,
                    num: res.data.maintenance_num,
                    describe: res.data.member_describe,
                })
                console.log(This.data.member_record)
            })
            
        } else if (This.data.type == 2){
            App._get('coach/Maintain/get_private_member_detail', {
                store_id: wx.getStorageSync('store_id'),
                openid: wx.getStorageSync('openid'),
                phone: wx.getStorageSync('phone'),
                member_id: This.data.id,
            }, function (res) {
                console.log(res)
                This.setData({
                    member_record: []
                })
                var action = { method: "pause" };
                for (var index in res.data.member_record) {
                    res.data.member_record[index].index1 = false;
                }
                This.setData({
                    caller: res.data,
                    num: res.data.maintenance_num,
                    member_record: res.data.member_record,
                    describe: res.data.member_describe,
                })
                console.log(res.data.member_record)
            })
            
        }
        
    },
    // 签到记录的显示点击
    layer_date(e) {
        var This = this;
        This.setData({
            flag_screen: false,
        })
    },
    hide_layer() {
        var This = this;
        This.setData({
            flag_screen: true
        })
    },
    // //修改
    // to_xiugai: function (e) {
    //     var This = this;
    //     wx.navigateTo({
    //         url: '../add_change/add_change?type=1' + '&id=' + e.currentTarget.dataset.id,
    //     })
    // },
    //跳转维护
    to_weihu:function(e){
        var This = this;
        console.log(e)
       wx.navigateTo({
           url: '../add_the_maintenance/add_the_maintenance?id=' + This.data.id + '&type=' + This.data.type,
       })
    },
    // 出勤記錄
    attendance_record(e) {
        var This = this;
        var years = This.data.year
        var months = This.data.month > 11 ? This.data.month : '0' + This.data.month
        var time = years + '-' + months
        // var data = {
        //     openid: wx.getStorageSync('openid'),
        //     phone: wx.getStorageSync('phone'),
        //     store_id: wx.getStorageSync('store_id'),
        //     id: This.data.id,
        //     // id: 220203,
        //     time: time,
        //     token: API_Key,
        // }
        // var str = coding(data);
        App._get('coach/Maintain/member_sign_log', {
            // signature: str,
            openid: wx.getStorageSync('openid'),
            phone: wx.getStorageSync('phone'),
            store_id: wx.getStorageSync('store_id'),
            member_id: This.data.id,
            // id: 220203,
            date_str: time,
        }, function (res) {
            console.log(res)
            if (res.status == 1) {
                console.log(This.data.dateArr)
                console.log(res.list)
                for (var i = 0; i < This.data.dateArr.length; i++) {
                    for (var j = 0; j < res.list.length; j++) {
                        if (This.data.dateArr[i].isToday == res.list[j]) {
                            This.data.dateArr[i].checked = true
                            This.setData({
                                dateArr: This.data.dateArr
                            })
                        }
                    }
                }
            }
        })
    },
    // 跳转课程私教课程详情页面
    to_class:function(e){
        var This = this;
        console.log(e)
        wx.navigateTo({
            url: '../details_private_education/details_private_education?member_id=' + This.data.id + '&item_id=' + e.currentTarget.dataset.id,
        })
    },
    ycxs: function (e) {
        var This = this;
        console.log(e)
        if (e.currentTarget.dataset.type == 1) {
            var tr = 2
        } else {
            var tr = 1
        }
        var pack = {
            openid: wx.getStorageSync('openid'),
            phone: wx.getStorageSync('phone'),
            store_id: wx.getStorageSync('store_id'),
            type: tr,
            caller_id: e.currentTarget.dataset.id,
            token: API_Key,
        }
        var api = coding(pack);
        App._get('caller/callerTypeExit', {
            store_id: wx.getStorageSync('store_id'),
            openid: wx.getStorageSync('openid'),
            phone: wx.getStorageSync('phone'),
            signature: api,
            caller_id: e.currentTarget.dataset.id,
            type: tr
        }, function (res) {
            console.log(res)
            if(res.status == 1){
                wx.showToast({
                    title: res.message,
                    duration: 2000
                })
                This.loade();
            }else{
                wx.showToast({
                    title: res.message,
                    duration: 2000
                })
            }
        })
    },
    dateInit: function (setYear, setMonth) {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let dateArr = []; //需要遍历的日历数组数据
        let arrLen = 0; //dateArr的数组长度
        let now = setYear ? new Date(setYear, setMonth) : new Date();
        let year = setYear || now.getFullYear();
        let nextYear = 0;
        let month = setMonth || now.getMonth(); //没有+1方便后面计算当月总天数
        let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
        let startWeek = new Date(year + '/' + (month + 1) + '/' + 1).getDay(); //目标月1号对应的星期
        let dayNums = new Date(year, nextMonth, 0).getDate(); //获取目标月有多少天
        let obj = {};
        let num = 0;

        if (month + 1 > 11) {
            nextYear = year + 1;
            dayNums = new Date(nextYear, nextMonth, 0).getDate();
        }
        arrLen = startWeek + dayNums;
        for (let i = 0; i < arrLen; i++) {
            if (i >= startWeek) {
                num = i - startWeek + 1;
                obj = {
                    isToday: '' + year + (month + 1) + num,
                    dateNum: num,
                    weight: 5,
                    checked: false
                }
            } else {
                obj = {
                    isToday: '',
                    dateNum: '',
                    weight: 5,
                    checked: false
                };
            }
            dateArr[i] = obj;
        }
        this.setData({
            dateArr: dateArr
        })
        console.log(this.data.dateArr)

        let nowDate = new Date();
        let nowYear = nowDate.getFullYear();
        let nowMonth = nowDate.getMonth() + 1;
        let nowWeek = nowDate.getDay();
        let getYear = setYear || nowYear;
        let getMonth = setMonth >= 0 ? (setMonth + 1) : nowMonth;

        if (nowYear == getYear && nowMonth == getMonth) {
            this.setData({
                isTodayWeek: true,
                todayIndex: nowWeek
            })
        } else {
            this.setData({
                isTodayWeek: false,
                todayIndex: -1
            })
        }
    },
    lastMonth: function () {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
        let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
        this.setData({
            year: year,
            month: (month + 1)
        })
        this.dateInit(year, month);
        this.attendance_record();
    },
    nextMonth: function () {
        //全部时间的月份都是按0~11基准，显示月份才+1
        let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
        let month = this.data.month > 11 ? 0 : this.data.month;
        this.setData({
            year: year,
            month: (month + 1)
        })
        this.dateInit(year, month);
        this.attendance_record();
    },
    preventTouchMove: function (e) { },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var This = this;
        This.loade();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        wx.pauseBackgroundAudio();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})